package com.toidep.fragment.others;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.VideoAdapter;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.entities.ModelEntity;
import com.toidep.widgets.sectionlist.AmazingListView;

public class ModelDetailVideoFragment extends BaseFragment {

	private MainActivity mainActivity;

	private View view;
	private RelativeLayout layoutNoData;
	private TextView textview_model_name, textview_video_total;
	private AmazingListView amazingListView;
	private VideoAdapter videoAdapter;
	
	private ModelEntity modelEntity;
	
	public void setValues(ModelEntity modelEntity) {
		this.modelEntity = modelEntity;
	}
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_MODEL;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_model));
		mainActivity.mainFragment.showButtonSlide(false);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mainActivity = (MainActivity) getActivity();
		view = inflater.inflate(R.layout.fragment_model_detail_tabs, container, false);

		initComponents();
		
		setBaseValues();
		
		requestData();
		
		return view;
	}

	private void initComponents() {
		
		((ImageView) view.findViewById(R.id.imageview_icon)).setImageResource(R.drawable.icon_model_detail_video);
		layoutNoData = (RelativeLayout) view.findViewById(R.id.layoutNoData);
		
		textview_model_name = (TextView) view.findViewById(R.id.textview_model_name);
		textview_video_total = (TextView) view.findViewById(R.id.textview_video_total);
		
		textview_model_name.setText(String.format(mainActivity.getString(R.string.text_model_video_title), modelEntity.getName()));
		textview_video_total.setVisibility(View.VISIBLE);
		textview_video_total.setText(String.format(mainActivity.getString(R.string.text_model_total_video), modelEntity.getTotalVideo()));
		
		// Khoi tao list model NEW
		amazingListView = (AmazingListView) view.findViewById(R.id.amazingListView);
		amazingListView.setAdapter(videoAdapter = new VideoAdapter(this, Constants.CATEGORY_MODEL_DETAL_VIDEO, modelEntity.getId()));
		amazingListView.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		videoAdapter.setAmazingListView(amazingListView, ConfigUtility.PAGE_SIZE);
		videoAdapter.notifyMayHaveMorePages();
		
		// Chuyen toi detail cua video
		amazingListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (videoAdapter != null && position >= 0) {
					videoAdapter.onItemClick(position);
				}
			}
		});
	}
	
	/**
	 * Goi API get list models
	 */
	private void requestData() {
		videoAdapter.resetList();
	}
	
	public static ModelDetailVideoFragment newInstance() {
		ModelDetailVideoFragment myFragment = new ModelDetailVideoFragment();

		return myFragment;
	}

}
